package com.yupi.web.job;

/**
 * @Author FengHuan Wang
 * @Date 2025/3/6 14:17
 * @Version 1.0
 */

import cn.hutool.core.util.StrUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yupi.web.manager.ObsManager;
import com.yupi.web.mapper.GeneratorMapper;
import com.yupi.web.model.entity.Generator;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务处理华为云obs对象删除处理类
 */
@Slf4j
public class ClearObsJobHandler {

    @Resource
    private ObsManager obsManager;

    @Resource
    private GeneratorMapper generatorMapper;

    /**
     * 每天执行删除操作
     */
    @XxlJob("clearObsJobHandler")
    public void clearObsJobHandler(){
        log.info("开始删除任务");
        //删除用户上传的模板制作文件
        obsManager.delObject("/file/");

        //已经删除的代码生成器对应的产物包文件
        List<Generator> generatorList = generatorMapper.selectDeletedData();
        generatorList.stream()
                .map(Generator::getDistPath)
                .filter(StrUtil::isNotBlank)
                .map(distPath -> distPath.substring(1))
                .collect(Collectors.toList());

        for (Generator generator : generatorList) {
            obsManager.delObject(generator.getDistPath());
        }

        log.info("结束删除");
    }
}
